package com.depth.viewer.utils.date
{
	import com.adobe.utils.DateUtil;
	import com.depth.viewer.utils.Util;
	
	import mx.formatters.DateFormatter;
	
	public class StringConvertDate extends DateFormatter
	{
		public function StringConvertDate()
		{
			super();
		}
		
		static public function convertToDate(valueString:String, inputFormat:String="YYYYMMDDJJNNSS"):Date
		{
			var mask:String
			var temp:String;
			var secondString:String = "";
			var minuteString:String = "";
			var hourString:String = "";
			var dateString:String = "";
			var monthString:String = "";
			var yearString:String = "";
			var j:int = 0;
			
			var n:int = inputFormat.length;
			for (var i:int = 0; i < n; i++,j++)
			{
				temp = "" + valueString.charAt(j);
				mask = "" + inputFormat.charAt(i);
				
				if (mask == "S")
				{
					if (isNaN(Number(temp)) || temp == " ")
						j--;
					else
						secondString += temp;
				}
				else if (mask == "N")
				{
					if (isNaN(Number(temp)) || temp == " ")
						j--;
					else
						minuteString += temp;
				}
				else if (mask == "J")
				{
					if (isNaN(Number(temp)) || temp == " ")
						j--;
					else
						hourString += temp;
				}
				else if (mask == "M")
				{
					if (isNaN(Number(temp)) || temp == " ")
						j--;
					else
						monthString += temp;
				}
				else if (mask == "D")
				{
					if (isNaN(Number(temp)) || temp == " ")
						j--;
					else
						dateString += temp;
				}
				else if (mask == "Y")
				{
					yearString += temp;
				}
				else if (!isNaN(Number(temp)) && temp != " ")
				{
					return null;
				}
			}
			
			temp = "" + valueString.charAt(inputFormat.length - i + j);
			if (!(temp == "") && (temp != " "))
				return null;
			var secondNum:Number = Number(secondString);
			var minuteNum:Number = Number(minuteString);
			var hourNum:Number = Number(hourString);
			var monthNum:Number = Number(monthString);
			var dayNum:Number = Number(dateString);
			var yearNum:Number = Number(yearString);
			
			if (isNaN(yearNum) || isNaN(monthNum) || isNaN(dayNum)||isNaN(hourNum)||isNaN(minuteNum)||isNaN(secondNum))
				return null;
			
			if (yearString.length == 2 && yearNum < 70)
				yearNum+=2000;
			
			var newDate:Date = new Date(yearNum, monthNum - 1, dayNum-1,hourNum,minuteNum,secondNum);
			
			if (dayNum != newDate.getDate() || (monthNum - 1) != newDate.getMonth())
				return null;
			
			return newDate;
		}
		/*
		public static function convertToDate(str:String):Date{
			if(str==null||str.length<14){
				return new Date();
			}
			var year:String = str.substr(0,4);
			var month:String = str.substr(4,2);
			var day:String  = str.substr(6,2);
			var hour:String = str.substr(8,2);
			var minute:String = str.substr(10,2);
			var second:String = str.substr(12,2);
			return new Date(year,month,day,hour,minute,second,0);
		}*/
	}
}